<?php
include ("header.php");

require_once("lib/user.php");
require_once("lib/unit.php");
require_once("lib/company.php");
require_once("lib/managefiledocument.php");
?>

<?php
$pageTitle = "View Document List";
$divId = "filedetails";
$handlerPage = "lib/managefiledocumenthandler.php";


$user= unserialize($_SESSION["user"]);
$actions=$user->getActions("GLOBAL");
$canViewAllDocuments=in_array("CAN_VIEW_ALL_DOCUMENTS",$actions);

//Get the document details of docuemnts
$filedocumentManager = new ManageFileDocuments ();
$documents=null;
if($canViewAllDocuments){
	$documents=$filedocumentManager->listDocuments();
}else {
	$userId=$user->getUserId();
	$documents=$filedocumentManager->listDocuments($userId);
}
?>
<script type="text/javascript">
	function updateDetails(){
		console.log("Updated");
	}
</script>

<!-- Start of main contents -->
<div class="maincontent">
	<!------- Header ---------->
	<div class="borderedDiv">
		<table style="width: 100%">
			<tr>
				<td class="titleText blueheadertext">View Documents</td>
			</tr>
		</table>
	</div>
	<!------- Content ---------->
	
	<?php
		//No of coumns in table 
		$columnCount=6; 
		if($canViewAllDocuments){
			$columnCount +=2;//extra columns for Admin
		}
	?>
	<div class="scroll-pane displaytable"  id="companydetails">
		<table class="contenttable">
			<thead>
				<tr>
					<td style="width:8%">Sl. No.</td>
					<td>File Name</td>
					<td>Uploaded On</td>
					<td>Size</td>
					<?php if($canViewAllDocuments){echo "<td>User Name</td>";} ?>
					<?php if($canViewAllDocuments){echo "<td>Unit</td>";} ?>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
				</tr>
			</thead>
	
			<tbody>
				<tr>
					<td colspan="<?php echo ($canViewAllDocuments?12:10); ?>" style="text-align: right;"><a href="#"
						onClick="javascript:createDialog('Add Document', '<?PHP echo $handlerPage;?>','<?PHP echo $handlerPage;?>','updateDetails()')"><span>Add
								Document</span>
						<div id="addUser" class="add" style="float: right;"></div> </a></td>
				</tr>
<?php
$i = 1;
foreach ( $documents as $document ) {
	$documentId=$document->getId();
	$userName=$document->getUserName();
	$unitName=$document->getUnit();
	$fileName=$document->getDocumentName();
	$uploadedOn=$document->getUploadedOn();
	$size=$document->getSize();
	$url=$document->getUrl();

	
	echo ("<tr>");
	echo ("<td>" . $i . "</td>");
	echo ('<td><a href="' .$url .'">' . $fileName . '</a></td>');
	echo ("<td>" . $uploadedOn . "</td>");
	echo ("<td>" . $size . "</td>");
	if($canViewAllDocuments){echo "<td>".$userName."</td>";} 
	if($canViewAllDocuments){echo "<td>".$unitName."</td>";}
	echo "<td><a id=\"documentid_" . $documentId . "\" alt=\"Edit\" href=\"#\" onClick=\"javascript:updateDialog('Update Document', '" . $handlerPage . "','" . $handlerPage . "'," . $documentId . ",'updateDetails()')\"><span>Edit Document</span><div class=\"modify\"></div></a></td>";
	echo "<td><a id=\"documentid_" . $documentId . "\" href=\"#\"  onClick=\"javascript:deleteDialog('Delete Document', '" . $handlerPage . "','" . $handlerPage . "'," . $documentId . ",'updateDetails()')\"><span>Delete Document</span><div class=\"delete\"></div></a></td>";
	echo ("</tr>");
	$i ++;
	
}
?>
			</tbody>
		</table>
	</div>
</div>
<!-- End of main contents -->

<!-- Footer -->
<?php
include ("footer.php");
?>